import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import store from '@/store'
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: '/home',
    component: ()=>import('@/views/home/indexView.vue')
  },
  {
    path: '/home',
    name: 'home',
    component: ()=>import('@/views/home/indexView.vue')
  },
  {
    path: '/list',
    name: 'list',
    component: ()=>import('@/views/list/indexView.vue')
  },
  {
    path: '/user',
    name: 'user',
    component: ()=>import('@/views/user/indexView.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: ()=>import('@/views/login/indexView.vue')
  },
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
// 使用路由守卫的知识，对“我的”面进行登录守卫
router.beforeEach((to,from,next)=>{
  const token=store.state.token
  if(!token&&to.path!=='/login'){
    next('/login')
  }
  next()
})

export default router
